﻿using TomShane.Neoforce.Controls;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using System.IO;
using System.Collections.Generic;

namespace Engine2D.Windows
{
    public class LibraryPanel : Window
    {
        private IList<Button> libraryElements;

        private Engine2D Engine { get { return (Engine2D)Manager.Game; } }
        private Texture2D SelectedTexture
        {
            get
            {
                foreach (Button ib in libraryElements)
                {
                    if (ib.Pushed)
                        return ib.Glyph.Image;
                }
                return null;
            }
        }

        public LibraryPanel(Manager manager)
            : base(manager)
        {
            AutoScroll = true;

            libraryElements = new List<Button>();
        }

        public override void Init()
        {
            base.Init();

            ToolBarPanel toolbarPanel = new ToolBarPanel(Manager);
            ToolBarPanel = toolbarPanel;

            ToolBar toolbar = new ToolBar(Manager);
            toolbar.FullRow = true;
            ToolBarPanel.Add(toolbar);

            ToolBarButton btnAdd = new ToolBarButton(Manager);
            btnAdd.Top = 1;
            btnAdd.Left = 10;
            btnAdd.Glyph = new Glyph(Manager.Content.Load<Texture2D>("ico_add"));
            btnAdd.Click += new EventHandler(btnAdd_Click);

            ToolBarButton btnRem = new ToolBarButton(Manager);
            btnRem.Top = 1;
            btnRem.Left = 35;
            btnRem.Glyph = new Glyph(Manager.Content.Load<Texture2D>("ico_rem"));
            btnRem.Click += new EventHandler(btnRem_Click);

            ToolBarButton btnIns = new ToolBarButton(Manager);
            btnIns.Top = 1;
            btnIns.Left = 60;
            btnIns.Glyph = new Glyph(Manager.Content.Load<Texture2D>("ico_down"));
            btnIns.Click += new EventHandler(btnIns_Click);

            toolbar.Add(btnAdd);
            toolbar.Add(btnRem);
            toolbar.Add(btnIns);

            Resize += new ResizeEventHandler(LibraryPanel_Resize);

            RefreshLibrary();
        }

        void btnRem_Click(object sender, EventArgs e)
        {
            foreach (Button ib in libraryElements)
            {
                if (ib.Pushed)
                {
                    Library library = Engine.Library;
                    library.Remove(ib.Glyph.Image);
                }
            }
            RefreshLibrary();
        }

        void LibraryPanel_Resize(object sender, EventArgs e)
        {
            ResizeButtons();
        }

        void btnAdd_Click(object sender, EventArgs e)
        {
            FileDialog filePicker = new FileDialog(Manager);
            filePicker.Init();
            filePicker.Closed += new WindowClosedEventHandler(filePicker_Closed);
            Parent.Add(filePicker);
        }

        void btnIns_Click(object sender, EventArgs e)
        {
            Texture2D texture = SelectedTexture;
            if (texture != null)
            {
                SceneObject obj = new SceneObject();
                obj.Texture = texture;
                obj.Position = new Vector3(0, 0, -2);
                Engine.Scene.AddSceneObject(obj);
            }
        }

        void filePicker_Closed(object sender, WindowClosedEventArgs e)
        {
            FileDialog filePicker = (FileDialog)sender;
            if (filePicker.ModalResult == ModalResult.Ok)
            {
                using (FileStream stream = new FileStream(filePicker.FileName, FileMode.Open))
                {
                    Texture2D texture = Texture2D.FromStream(Manager.GraphicsDevice, stream);

                    Engine2D engine = (Engine2D)Manager.Game;
                    Library library = engine.Library;
                    library.Add(texture);
                    RefreshLibrary();
                }
            }
        }

        private void RefreshLibrary()
        {
            foreach (Button ib in libraryElements)
            {
                Remove(ib);
            }
            libraryElements.Clear();

            Engine2D engine = (Engine2D)Manager.Game;
            Library library = engine.Library;
            int boxY = 0;
            foreach (Texture2D texture in library)
            {
                Button ib = new Button(Manager);
                ib.Mode = ButtonMode.PushButton;
                ib.Glyph = new Glyph(texture);
                ib.Click += new EventHandler(ib_Click);
                ib.Init();
                Add(ib);
                libraryElements.Add(ib);

                boxY += ClientWidth;
            }

            ResizeButtons();
        }

        void ib_Click(object sender, EventArgs e)
        {
            foreach (Button ib in libraryElements)
            {
                ib.Pushed = false;
            }
            Button ib1 = (Button)sender;
            ib1.Pushed = true;
        }

        private void ResizeButtons()
        {
            int boxY = 0;
            foreach (Button ib in libraryElements)
            {
                ib.Left = 0;
                ib.Top = boxY;
                ib.Width = ClientWidth;
                ib.Height = ClientWidth;
                boxY += ClientWidth;
            }
        }
    }
}
